// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// c [author] rhyme_lph
// K [email]  rhymelph@gmail.com
// J [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/entities/address_entity.dart';
import 'package:finance_app/src/entities/country_entity.dart';
import 'package:finance_app/src/http_service/http_common_service.dart';
import 'package:finance_app/src/http_service/http_quotation_service.dart';
import 'package:finance_app/src/widgets/bottom_sheet/address_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/country_bottom_sheet.dart';
import 'package:finance_app/src/widgets/common/pdf_watch_view.dart';
import 'package:flutter/material.dart';

class DeliveryOrderProvider extends BaseProvider {
  bool isShowNext = true;
  GlobalKey<PdfWatchViewState> pdfKey = GlobalKey();

  List<String> menuList = [
    'Edit',
    'Share',
  ];

  CountryEntity country;

  String address;

  TextEditingController unitNoController = TextEditingController();
  FocusNode unitNoFocusNode = FocusNode();

  TextEditingController postalCodeController = TextEditingController();
  FocusNode postalCodeFocusNode = FocusNode();

  int id;

  @override
  void dispose() {
    super.dispose();
    unitNoController.dispose();
    unitNoFocusNode.dispose();
    postalCodeController.dispose();
    postalCodeFocusNode.dispose();
  }

  void unFocus() {
    postalCodeFocusNode.unfocus();
    unitNoFocusNode.unfocus();
  }

  @override
  void initState() {
    super.initState();
  }

  void onSelectMenu(String value) {
    switch (value) {
      case 'Edit':
        isShowNext = true;
        update();
        break;
      case 'Share':
        onShareDeliverOrder();
        break;
    }
  }

  @override
  Future get dataFuture => null;

  void onNext() async {
    if (unitNoController.text.length == 0) {
      showToast(S.current.please_enter_unit_no);
      return;
    }
    if (postalCodeController.text.length == 0) {
      showToast(S.current.please_enter_postal_code);
      return;
    }
    if (country == null) {
      showToast(S.current.please_select_country);
      return;
    }
    if (address == null) {
      showToast(S.current.please_select_address);
      return;
    }
    bool isSuccess = false;
    await post(
        HttpQuotationService.deliveryQuotation(id,
            address_city: country?.key,
            address_country: country?.key,
            address_line_1: address,
            address_postal_code: postalCodeController.text,
            address_unit: unitNoController.text),
        autoHandler: AutoHandler.Toast,
        loadingTip: S.current.loading, onSuccess: (result) async {
      isSuccess = true;
    });
    if (isSuccess) {
      isShowNext = !isShowNext;
      update();
    }
  }

  void onSelectCountry() {
    obtainContext?.call((context) async {
      final _country = await showCountryBottomSheet(
        context,
        country,
      );
      if (_country != null) {
        country = _country;
        update();
      }
    });
  }

  void onSelectAddress() {
    unFocus();
    obtainContext?.call((context) async {
      AddressEntity entity = await showAddressBottomSheet(context, null,
          (String searchData) async {
        List<AddressEntity> addressList;
        await post(HttpCommonService.searchAddress(searchData),
            autoHandler: AutoHandler.Toast, onSuccess: (result) {
          addressList = getEntityFromResult<List<AddressEntity>>(result);
        });
        if (addressList == null) {
          addressList = [];
        }
        return addressList;
      });
      if (entity != null) {
        address = entity.address;
        postalCodeController.text = entity.code;
        update();
      }
    });
  }

  void onShareDeliverOrder() async {
    pdfKey.currentState.onShare();
  }
}
